package com.sy.server.mapper;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.sy.server.model.Cost;
import com.sy.server.model.CostExample.Criteria;
import com.sy.server.model.CostExample.Criterion;
import com.sy.server.model.CostExample;
import java.util.List;
import java.util.Map;

public class CostSqlProvider {

    public String countByExample(CostExample example) {
        BEGIN();
        SELECT("count(*)");
        FROM("cost");
        applyWhere(example, false);
        return SQL();
    }

    public String deleteByExample(CostExample example) {
        BEGIN();
        DELETE_FROM("cost");
        applyWhere(example, false);
        return SQL();
    }

    public String insertSelective(Cost record) {
        BEGIN();
        INSERT_INTO("cost");
        
        if (record.getUserId() != null) {
            VALUES("user_id", "#{userId,jdbcType=INTEGER}");
        }
        
        if (record.getDay() != null) {
            VALUES("day", "#{day,jdbcType=VARCHAR}");
        }
        
        if (record.getEat() != null) {
            VALUES("eat", "#{eat,jdbcType=DOUBLE}");
        }
        
        if (record.getEatDetail() != null) {
            VALUES("eat_detail", "#{eatDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getHouse() != null) {
            VALUES("house", "#{house,jdbcType=DOUBLE}");
        }
        
        if (record.getHouseDetail() != null) {
            VALUES("house_detail", "#{houseDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getCommunication() != null) {
            VALUES("communication", "#{communication,jdbcType=DOUBLE}");
        }
        
        if (record.getCommunicationDetail() != null) {
            VALUES("communication_detail", "#{communicationDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getTraffic() != null) {
            VALUES("traffic", "#{traffic,jdbcType=DOUBLE}");
        }
        
        if (record.getTrafficDetail() != null) {
            VALUES("traffic_detail", "#{trafficDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getDaily() != null) {
            VALUES("daily", "#{daily,jdbcType=DOUBLE}");
        }
        
        if (record.getDailyDetail() != null) {
            VALUES("daily_detail", "#{dailyDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getEducation() != null) {
            VALUES("education", "#{education,jdbcType=DOUBLE}");
        }
        
        if (record.getEducationDetail() != null) {
            VALUES("education_detail", "#{educationDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getClothes() != null) {
            VALUES("clothes", "#{clothes,jdbcType=DOUBLE}");
        }
        
        if (record.getClothesDetail() != null) {
            VALUES("clothes_detail", "#{clothesDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getSnacks() != null) {
            VALUES("snacks", "#{snacks,jdbcType=DOUBLE}");
        }
        
        if (record.getSnacksDetail() != null) {
            VALUES("snacks_detail", "#{snacksDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getFruits() != null) {
            VALUES("fruits", "#{fruits,jdbcType=DOUBLE}");
        }
        
        if (record.getFruitsDetail() != null) {
            VALUES("fruits_detail", "#{fruitsDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getMedical() != null) {
            VALUES("medical", "#{medical,jdbcType=DOUBLE}");
        }
        
        if (record.getMedicalDetail() != null) {
            VALUES("medical_detail", "#{medicalDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getOthers() != null) {
            VALUES("others", "#{others,jdbcType=DOUBLE}");
        }
        
        if (record.getOthersDetail() != null) {
            VALUES("others_detail", "#{othersDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getSum() != null) {
            VALUES("sum", "#{sum,jdbcType=DOUBLE}");
        }
        
        if (record.getTodayEvent() != null) {
            VALUES("today_event", "#{todayEvent,jdbcType=VARCHAR}");
        }
        
        return SQL();
    }

    public String selectByExample(CostExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("user_id");
        } else {
            SELECT("user_id");
        }
        SELECT("day");
        SELECT("eat");
        SELECT("eat_detail");
        SELECT("house");
        SELECT("house_detail");
        SELECT("communication");
        SELECT("communication_detail");
        SELECT("traffic");
        SELECT("traffic_detail");
        SELECT("daily");
        SELECT("daily_detail");
        SELECT("education");
        SELECT("education_detail");
        SELECT("clothes");
        SELECT("clothes_detail");
        SELECT("snacks");
        SELECT("snacks_detail");
        SELECT("fruits");
        SELECT("fruits_detail");
        SELECT("medical");
        SELECT("medical_detail");
        SELECT("others");
        SELECT("others_detail");
        SELECT("sum");
        SELECT("today_event");
        FROM("cost");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        ORDER_BY("day desc");
        
        return SQL();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        Cost record = (Cost) parameter.get("record");
        CostExample example = (CostExample) parameter.get("example");
        
        BEGIN();
        UPDATE("cost");
        
        if (record.getUserId() != null) {
            SET("user_id = #{record.userId,jdbcType=INTEGER}");
        }
        
        if (record.getDay() != null) {
            SET("day = #{record.day,jdbcType=VARCHAR}");
        }
        
        if (record.getEat() != null) {
            SET("eat = #{record.eat,jdbcType=DOUBLE}");
        }
        
        if (record.getEatDetail() != null) {
            SET("eat_detail = #{record.eatDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getHouse() != null) {
            SET("house = #{record.house,jdbcType=DOUBLE}");
        }
        
        if (record.getHouseDetail() != null) {
            SET("house_detail = #{record.houseDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getCommunication() != null) {
            SET("communication = #{record.communication,jdbcType=DOUBLE}");
        }
        
        if (record.getCommunicationDetail() != null) {
            SET("communication_detail = #{record.communicationDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getTraffic() != null) {
            SET("traffic = #{record.traffic,jdbcType=DOUBLE}");
        }
        
        if (record.getTrafficDetail() != null) {
            SET("traffic_detail = #{record.trafficDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getDaily() != null) {
            SET("daily = #{record.daily,jdbcType=DOUBLE}");
        }
        
        if (record.getDailyDetail() != null) {
            SET("daily_detail = #{record.dailyDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getEducation() != null) {
            SET("education = #{record.education,jdbcType=DOUBLE}");
        }
        
        if (record.getEducationDetail() != null) {
            SET("education_detail = #{record.educationDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getClothes() != null) {
            SET("clothes = #{record.clothes,jdbcType=DOUBLE}");
        }
        
        if (record.getClothesDetail() != null) {
            SET("clothes_detail = #{record.clothesDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getSnacks() != null) {
            SET("snacks = #{record.snacks,jdbcType=DOUBLE}");
        }
        
        if (record.getSnacksDetail() != null) {
            SET("snacks_detail = #{record.snacksDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getFruits() != null) {
            SET("fruits = #{record.fruits,jdbcType=DOUBLE}");
        }
        
        if (record.getFruitsDetail() != null) {
            SET("fruits_detail = #{record.fruitsDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getMedical() != null) {
            SET("medical = #{record.medical,jdbcType=DOUBLE}");
        }
        
        if (record.getMedicalDetail() != null) {
            SET("medical_detail = #{record.medicalDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getOthers() != null) {
            SET("others = #{record.others,jdbcType=DOUBLE}");
        }
        
        if (record.getOthersDetail() != null) {
            SET("others_detail = #{record.othersDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getSum() != null) {
            SET("sum = #{record.sum,jdbcType=DOUBLE}");
        }
        
        if (record.getTodayEvent() != null) {
            SET("today_event = #{record.todayEvent,jdbcType=VARCHAR}");
        }
        
        applyWhere(example, true);
        return SQL();
    }

    public String updateByExample(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("cost");
        
        SET("user_id = #{record.userId,jdbcType=INTEGER}");
        SET("day = #{record.day,jdbcType=VARCHAR}");
        SET("eat = #{record.eat,jdbcType=DOUBLE}");
        SET("eat_detail = #{record.eatDetail,jdbcType=VARCHAR}");
        SET("house = #{record.house,jdbcType=DOUBLE}");
        SET("house_detail = #{record.houseDetail,jdbcType=VARCHAR}");
        SET("communication = #{record.communication,jdbcType=DOUBLE}");
        SET("communication_detail = #{record.communicationDetail,jdbcType=VARCHAR}");
        SET("traffic = #{record.traffic,jdbcType=DOUBLE}");
        SET("traffic_detail = #{record.trafficDetail,jdbcType=VARCHAR}");
        SET("daily = #{record.daily,jdbcType=DOUBLE}");
        SET("daily_detail = #{record.dailyDetail,jdbcType=VARCHAR}");
        SET("education = #{record.education,jdbcType=DOUBLE}");
        SET("education_detail = #{record.educationDetail,jdbcType=VARCHAR}");
        SET("clothes = #{record.clothes,jdbcType=DOUBLE}");
        SET("clothes_detail = #{record.clothesDetail,jdbcType=VARCHAR}");
        SET("snacks = #{record.snacks,jdbcType=DOUBLE}");
        SET("snacks_detail = #{record.snacksDetail,jdbcType=VARCHAR}");
        SET("fruits = #{record.fruits,jdbcType=DOUBLE}");
        SET("fruits_detail = #{record.fruitsDetail,jdbcType=VARCHAR}");
        SET("medical = #{record.medical,jdbcType=DOUBLE}");
        SET("medical_detail = #{record.medicalDetail,jdbcType=VARCHAR}");
        SET("others = #{record.others,jdbcType=DOUBLE}");
        SET("others_detail = #{record.othersDetail,jdbcType=VARCHAR}");
        SET("sum = #{record.sum,jdbcType=DOUBLE}");
        SET("today_event = #{record.todayEvent,jdbcType=VARCHAR}");
        
        CostExample example = (CostExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    public String updateByPrimaryKeySelective(Cost record) {
        BEGIN();
        UPDATE("cost");
        
        if (record.getEat() != null) {
            SET("eat = #{eat,jdbcType=DOUBLE}");
        }
        
        if (record.getEatDetail() != null) {
            SET("eat_detail = #{eatDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getHouse() != null) {
            SET("house = #{house,jdbcType=DOUBLE}");
        }
        
        if (record.getHouseDetail() != null) {
            SET("house_detail = #{houseDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getCommunication() != null) {
            SET("communication = #{communication,jdbcType=DOUBLE}");
        }
        
        if (record.getCommunicationDetail() != null) {
            SET("communication_detail = #{communicationDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getTraffic() != null) {
            SET("traffic = #{traffic,jdbcType=DOUBLE}");
        }
        
        if (record.getTrafficDetail() != null) {
            SET("traffic_detail = #{trafficDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getDaily() != null) {
            SET("daily = #{daily,jdbcType=DOUBLE}");
        }
        
        if (record.getDailyDetail() != null) {
            SET("daily_detail = #{dailyDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getEducation() != null) {
            SET("education = #{education,jdbcType=DOUBLE}");
        }
        
        if (record.getEducationDetail() != null) {
            SET("education_detail = #{educationDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getClothes() != null) {
            SET("clothes = #{clothes,jdbcType=DOUBLE}");
        }
        
        if (record.getClothesDetail() != null) {
            SET("clothes_detail = #{clothesDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getSnacks() != null) {
            SET("snacks = #{snacks,jdbcType=DOUBLE}");
        }
        
        if (record.getSnacksDetail() != null) {
            SET("snacks_detail = #{snacksDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getFruits() != null) {
            SET("fruits = #{fruits,jdbcType=DOUBLE}");
        }
        
        if (record.getFruitsDetail() != null) {
            SET("fruits_detail = #{fruitsDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getMedical() != null) {
            SET("medical = #{medical,jdbcType=DOUBLE}");
        }
        
        if (record.getMedicalDetail() != null) {
            SET("medical_detail = #{medicalDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getOthers() != null) {
            SET("others = #{others,jdbcType=DOUBLE}");
        }
        
        if (record.getOthersDetail() != null) {
            SET("others_detail = #{othersDetail,jdbcType=VARCHAR}");
        }
        
        if (record.getSum() != null) {
            SET("sum = #{sum,jdbcType=DOUBLE}");
        }
        
        if (record.getTodayEvent() != null) {
            SET("today_event = #{todayEvent,jdbcType=VARCHAR}");
        }
        
        WHERE("user_id = #{userId,jdbcType=INTEGER}");
        WHERE("day = #{day,jdbcType=VARCHAR}");
        
        return SQL();
    }

    protected void applyWhere(CostExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }
}